#include <algorithm>
#include <cstdio>
#include <cstring>

int n;
int map[9][9], sum[9][9], f[9][9][9][9][16];

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("P1436 棋盘分割.in", "r", stdin);
  freopen("P1436 棋盘分割.out", "w", stdout);
#endif
#endif

  scanf("%d", &n);
  for (int i = 1; i <= 8; ++i) {
    for (int j = 1; j <= 8; ++j) {
      scanf("%d", &sum[i][j]);
      sum[i][j] += sum[i][j - 1];
    }
    for (int j = 1; j <= 8; ++j) sum[i][j] += sum[i - 1][j];
  }
  memset(f, 0x3f, sizeof(f));
  for (int i = 1; i <= 8; ++i)
    for (int j = 1; j <= 8; ++j)
      for (int k = i; k <= 8; ++k)
        for (int h = j; h <= 8; ++h)
          f[i][j][k][h][0] = (sum[k][h] - sum[k][j - 1] - sum[i - 1][h] + sum[i - 1][j - 1]) *
                             (sum[k][h] - sum[k][j - 1] - sum[i - 1][h] + sum[i - 1][j - 1]);
  for (int c = 1; c < n; ++c) {
    for (int i = 1; i <= 8; ++i)
      for (int j = 1; j <= 8; ++j)
        for (int k = i; k <= 8; ++k)
          for (int h = j; h <= 8; ++h) {
            for (int a = i; a < k; ++a)
              f[i][j][k][h][c] =
                  std::min(f[i][j][k][h][c], std::min(f[i][j][a][h][0] + f[a + 1][j][k][h][c - 1],
                                                      f[i][j][a][h][c - 1] + f[a + 1][j][k][h][0]));
            for (int a = j; a < h; ++a)
              f[i][j][k][h][c] =
                  std::min(f[i][j][k][h][c], std::min(f[i][j][k][a][0] + f[i][a + 1][k][h][c - 1],
                                                      f[i][j][k][a][c - 1] + f[i][a + 1][k][h][0]));
          }
  }
  printf("%d", f[1][1][8][8][n - 1]);
  return 0;
}